package com.bigluo.controller;

import com.alibaba.fastjson.JSON;
import com.bigluo.api.BaseResponse;
import com.bigluo.api.rsp.QryAllUsrInfoRsp;
import com.bigluo.dao.domain.User;
import com.bigluo.service.FileUploadService;
import com.bigluo.service.UserService;
import com.bigluo.util.Assert;
import com.sun.xml.internal.rngom.parse.host.Base;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * Created by luojun on 2017/9/8.
 * 文件上传下载Controller
 */
@RequestMapping("uploadService")
@Controller
public class FileUploadController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private FileUploadService fileUploadService;
    @RequestMapping("upload")
    public String upload() {
        return "uploadPage";
    }

    @RequestMapping("uploadFile")
    public String uploadFile(@RequestParam("name") String fileName,@RequestParam("file") MultipartFile file) throws Exception{
        logger.info("上传文件名：{}",fileName);
        if(!file.isEmpty()) {
            String oriName = file.getOriginalFilename();
            if(oriName.contains(".xlsx")||oriName.contains(".xls")) {
                InputStream in = file.getInputStream();
                BaseResponse rsp = fileUploadService.dealFile(in);
                logger.info("文件上传处理文件结果：{}", JSON.toJSONString(rsp));
            }else{
                File destFile = new File("d:/temppic/"+file.getOriginalFilename());
                if(!destFile.exists()) {
                    destFile.mkdirs();
                }
                file.transferTo(destFile);
                logger.info("上传文件成功");
            }
            return "success";
        }else {
            return "fail";
        }
    }

}
